﻿using MPSystem.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using Ay.MvcFramework;
using Ay.Framework.DataCreaters;
using Ay.Framework.WPF.Controls;
using Ay.Framework.WPF;
using Newtonsoft.Json;
using System.Windows.Threading;
using System.Windows.Navigation;
using System.ComponentModel;
using Base.BLL;
using Base.DAL;
using Base.DAL;

namespace MPSystem.Controllers
{
    public class BasicDataSet_SSController : Controller
    {

        Common common = new Common();
        private bool _IsBusy = false;
        public bool IsBusy
        {
            get { return _IsBusy; }
            set { Set(ref _IsBusy, value); }
        }



        #region 基本信息设置
        //赋值
        public ObservableCollection<BasicDataSet_SSModel> DataS1 { get; set; } = new ObservableCollection<BasicDataSet_SSModel>();
        //初始化用
        public BasicDataSet_SSModel S1Model { get; set; } = new BasicDataSet_SSModel();
        // <summary>
        /// 选择串口
        /// </summary>
        private ObservableCollection<IAyCheckedItem> _ComCbo = new ObservableCollection<IAyCheckedItem>();

        public ObservableCollection<IAyCheckedItem> ComCbo
        {
            get { return _ComCbo; }
            set { Set(ref _ComCbo, value); }
        }
        #endregion

        #region 测区
        public ObservableCollection<CQTable> DataSCQ { get; set; } = new ObservableCollection<CQTable>();
        //初始化用
        public CQModel cqm { get; set; } = new CQModel();
        // <summary>
        /// 选择串口
        /// </summary>
        private ObservableCollection<IAyCheckedItem> _SYCbo = new ObservableCollection<IAyCheckedItem>();

        public ObservableCollection<IAyCheckedItem> SYCbo
        {
            get { return _SYCbo; }
            set { Set(ref _SYCbo, value); }
        }
        #endregion

        #region 工作面
        public ObservableCollection<GZMTable> DataSGZM { get; set; } = new ObservableCollection<GZMTable>();
        //初始化用
        public GZMModel gzmm { get; set; } = new GZMModel();

        /// 选择串口
        /// </summary>
        private ObservableCollection<IAyCheckedItem> _CQCbo = new ObservableCollection<IAyCheckedItem>();

        public ObservableCollection<IAyCheckedItem> CQCbo
        {
            get { return _CQCbo; }
            set { Set(ref _CQCbo, value); }
        }

        #endregion

        #region 巷道
        public ObservableCollection<XDTable> DataSXD { get; set; } = new ObservableCollection<XDTable>();
        //初始化用
        public XDModel xdm { get; set; } = new XDModel();

        #endregion
        public BasicDataSet_SSController() : base()
        {
            var com = Db.Query<SYSTEMCOMB>();
            common.LoadCombox_IE(ComCbo, com);
            common.LoadCombox_Status(SYCbo);//状态
            var cq = ConfigService.GetCQ(false);
            common.LoadCombox_IE(CQCbo, cq);


            LoadData();

            #region  基本信息

            browse = inParam =>
            {
                string tmp_path = "";
                System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
                fbd.ShowDialog();
                if (fbd.SelectedPath != string.Empty)
                {
                    tmp_path = fbd.SelectedPath;
                    S1Model.Bflj = tmp_path;
                }
            };

            comSetCmd = inParam =>
            {
                ShowNavigationWindow("串口配置", "/Views/BasicDataSet/BasicDataSet_COM.xaml", 600, 500);
            };

            Save = inParam =>
            {
                if (S1Model.Kjmc == null || S1Model.Kjmc == "")
                {
                    AyMessageBox.ShowError("请填写公司名称！");
                    return;
                }
                if (S1Model.Bflj == null || S1Model.Bflj == "")
                {
                    AyMessageBox.ShowError("请选择备份路径！");
                    return;
                }
                SYSTEMKMB kjb = new SYSTEMKMB();
                kjb.KMBH = S1Model.Kjbh;
                kjb.TELNUM = S1Model.Dhhm;
                kjb.PATH = S1Model.Bflj;
                kjb.KM = S1Model.Kjmc;
                kjb.COM = S1Model.Com;

                if (!ConfigService.SetKM(kjb))
                    AyMessageBox.ShowError("保存失败！");
                else
                {
                    AyMessageBox.ShowInformation("保存成功！");
                }
            };

            #endregion


            #region 增加/修改测区
            AddCQ = inParam =>
            {
                //判断串口是否已被其他测区使用
                foreach (var _cq in DataSCQ)
                {
                    if (_cq.Ckmc == cqm.Ckmc)
                    {
                        AyMessageBox.ShowError("串口已被其他测区使用！");
                        return;
                    }
                }
                //var _1 = cqm;// inParam as CQTable;
                SYSTEMCQB cqb = new SYSTEMCQB();
                cqb.KMBH = Db.Find<SYSTEMKMB>().KMBH;
                cqb.CQMC = cqm.Cqmc;
                cqb.COM = cqm.Ckmc;
                cqb.ZT = cqm.Syzt;
                if (cqb.CQMC == null || cqb.CQMC == "")
                {
                    AyMessageBox.ShowError("请填写测区名称！");
                    return;
                }
                if (cqb.COM == "-1")
                {
                    AyMessageBox.ShowError("请选择串口！");
                    return;
                }
                if (cqb.ZT == -1)
                {
                    AyMessageBox.ShowError("请选择使用状态！");
                    return;
                }
                if (!ConfigService.AddCQ(cqb))
                    AyMessageBox.ShowError("保存失败！");
                else
                {
                    AyMessageBox.ShowInformation("保存成功！");
                    var cqs = ConfigService.GetCQ();
                    common.LoadCombox_IE(CQCbo, cqs);
                    LoadData();
                }
            };
            EditCQ = inParam =>
            {
                //判断串口是否已被其他测区使用
                foreach (var _cq in DataSCQ)
                {
                    if (_cq.Cqbh != cqm.Cqbh && _cq.Ckmc == cqm.Ckmc)
                    {
                        AyMessageBox.ShowError("串口已被其他测区使用！");
                        return;
                    }
                }
                SYSTEMCQB cqb = new SYSTEMCQB();
                cqb.KMBH = Db.Find<SYSTEMKMB>().KMBH;
                cqb.CQBH = cqm.Cqbh;
                cqb.CQMC = cqm.Cqmc;
                cqb.COM = cqm.Ckmc;
                cqb.ZT = cqm.Syzt;
                if (cqb.CQMC == null || cqb.CQMC == "")
                {
                    AyMessageBox.ShowError("请填写测区名称！");
                    return;
                }
                if (cqb.COM == "-1")
                {
                    AyMessageBox.ShowError("请选择串口！");
                    return;
                }
                if (cqb.ZT == -1)
                {
                    AyMessageBox.ShowError("请选择使用状态！");
                    return;
                }
                if (!ConfigService.SetCQ(cqb))
                    AyMessageBox.ShowError("保存失败！");
                else
                {
                    AyMessageBox.ShowInformation("保存成功！");
                    var cqs = ConfigService.GetCQ();
                    common.LoadCombox_IE(CQCbo, cqs);
                    LoadData();
                }
            };
            DoubleRowCQ = inParam =>
            {
                if (inParam == null)
                    return;
                var _1 = inParam as CQTable;
                cqm.Ckmc = _1.Ckmc;
                cqm.Cqmc = _1.Cqmc;
                cqm.Syzt = _1.Syzt;
                cqm.Cqbh = _1.Cqbh;
            };
            #endregion

            #region 增加/修改工作面
            AddGZM = inParam =>
            {
                //判断工作面名称是否已被其他使用
                foreach (var _gzm in DataSGZM)
                {
                    if (_gzm.Gzmmc == gzmm.Gzmmc)
                    {
                        AyMessageBox.ShowError("工作面名称已被使用！");
                        return;
                    }
                }
                SYSTEMGZMB gzmb = new SYSTEMGZMB();
                gzmb.CQBH = gzmm.Sscq;
                gzmb.GZMMC = gzmm.Gzmmc;
                //gzmb.GZMBH = 11;//需要手动获取
                gzmb.ZT = gzmm.Syzt;

                if (gzmb.CQBH == -1)
                {
                    AyMessageBox.ShowError("请选择测区！");
                    return;
                }
                if (gzmb.GZMMC == null || gzmb.GZMMC == "")
                {
                    AyMessageBox.ShowError("请填写工作面名称！");
                    return;
                }
                if (gzmb.ZT == -1)
                {
                    AyMessageBox.ShowError("请选择使用状态！");
                    return;
                }

                if (!ConfigService.AddGZM(gzmb))
                    AyMessageBox.ShowError("保存失败！");
                else
                {
                    AyMessageBox.ShowInformation("保存成功！");
                    LoadDataGZM();
                }
            };
            EditGZM = inParam =>
            {
                //判断工作面名称是否已被其他使用
                foreach (var _gzm in DataSGZM)
                {
                    if (_gzm.Gzmbh != gzmm.Gzmbh && _gzm.Gzmmc == gzmm.Gzmmc)
                    {
                        AyMessageBox.ShowError("工作面名称已被使用！");
                        return;
                    }
                }
                SYSTEMGZMB gzmb = new SYSTEMGZMB();
                gzmb.CQBH = gzmm.Sscq;
                gzmb.GZMMC = gzmm.Gzmmc;
                gzmb.GZMBH = gzmm.Gzmbh;
                gzmb.ZT = gzmm.Syzt;

                if (gzmb.CQBH == -1)
                {
                    AyMessageBox.ShowError("请选择测区！");
                    return;
                }
                if (gzmb.GZMMC == null || gzmb.GZMMC == "")
                {
                    AyMessageBox.ShowError("请填写工作面名称！");
                    return;
                }
                if (gzmb.ZT == -1)
                {
                    AyMessageBox.ShowError("请选择使用状态！");
                    return;
                }

                if (!ConfigService.SetGZM(gzmb))
                    AyMessageBox.ShowError("保存失败！");
                else
                {
                    AyMessageBox.ShowInformation("保存成功！");
                    LoadDataGZM();

                }

            };
            DoubleRowGZM = inParam =>
            {
                if (inParam == null)
                    return;
                var r = inParam as GZMTable;
                gzmm.Gzmmc = r.Gzmmc;
                gzmm.Gzmbh = r.Gzmbh;
                gzmm.Syzt = r.Syzt;
                gzmm.Sscq = r.Sscq;

            };
            #endregion

            #region 增加/修改巷道
            AddXD = inParam =>
            {
                //判断巷道名称是否已被其他使用
                foreach (var _xd in DataSXD)
                {
                    if (_xd.Xdmc == xdm.Xdmc)
                    {
                        AyMessageBox.ShowError("巷道名称已被使用！");
                        return;
                    }
                }

                SYSTEMXDB xdb = new SYSTEMXDB();
                xdb.CQBH = xdm.Sscq;
                xdb.XDMC = xdm.Xdmc;
                //xdb.XDBH = 11;//需要手动获取
                xdb.ZT = xdm.Syzt;

                if (xdb.CQBH == -1)
                {
                    AyMessageBox.ShowError("请选择测区！");
                    return;
                }
                if (xdb.XDMC == null || xdb.XDMC == "")
                {
                    AyMessageBox.ShowError("请填写巷道名称！");
                    return;
                }
                if (xdb.ZT == -1)
                {
                    AyMessageBox.ShowError("请选择使用状态！");
                    return;
                }

                if (!ConfigService.AddXD(xdb))
                    AyMessageBox.ShowError("保存失败！");
                else
                {
                    AyMessageBox.ShowInformation("保存成功！");
                    LoadDataXD();
                }


            };
            EditXD = inParam =>
            {
                //判断巷道名称是否已被其他使用
                foreach (var _xd in DataSXD)
                {
                    if (_xd.Xdbh != xdm.Xdbh && _xd.Xdmc == xdm.Xdmc)
                    {
                        AyMessageBox.ShowError("巷道名称已被使用！");
                        return;
                    }
                }

                SYSTEMXDB xdb = new SYSTEMXDB();
                xdb.CQBH = xdm.Sscq;
                xdb.XDMC = xdm.Xdmc;
                xdb.XDBH = xdm.Xdbh;
                xdb.ZT = xdm.Syzt;

                if (xdb.CQBH == -1)
                {
                    AyMessageBox.ShowError("请选择测区！");
                    return;
                }
                if (xdb.XDMC == null || xdb.XDMC == "")
                {
                    AyMessageBox.ShowError("请填写巷道名称！");
                    return;
                }
                if (xdb.ZT == -1)
                {
                    AyMessageBox.ShowError("请选择使用状态！");
                    return;
                }

                if (!ConfigService.SetXD(xdb))
                    AyMessageBox.ShowError("保存失败！");
                else
                {
                    AyMessageBox.ShowInformation("保存成功！");
                    LoadDataXD();
                }


            };
            DoubleRowXD = inParam =>
            {
                if (inParam == null)
                    return;
                var _1 = inParam as XDTable;
                xdm.Xdmc = _1.Xdmc;
                xdm.Sscq = _1.Sscq;
                xdm.Syzt = _1.Syzt;
                xdm.Xdbh = _1.Xdbh;


            };
            #endregion



        }


        /// <summary>
        /// 保存矿井
        /// </summary>
        public ActionResult Save { get; private set; } = inParam =>
        {

        };
        /// <summary>
        /// 浏览路径
        /// </summary>
        public ActionResult browse { get; private set; } = inParam =>
        {

        };
        /// <summary>
        /// 串口设置
        /// </summary>
        public ActionResult comSetCmd { get; private set; } = inParam =>
        {

        };

        /// <summary>/// 弹出层对象/// </summary>

        public static NavigationWindow window = null;
        /// <summary>/// 使用NavigationWindow弹出页面/// </summary>

        private void ShowNavigationWindow(string title, string uri, int width, int height)
        {
            window = new NavigationWindow();
            window.Title = title;
            window.Width = width;
            window.Height = height;
            window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            window.ResizeMode = ResizeMode.NoResize;
            window.Source = new Uri(uri, UriKind.Relative);
            window.ShowsNavigationUI = false;
            window.Closing += Window_Closing1;
            ConditonsModel.Name = cqm.Ckmc;
            window.ShowDialog();
        }

        private void Window_Closing1(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //AyMessageBox.ShowError("aa");

        }


        #region 测区



        public ActionResult AddCQ { get; private set; } = inParam =>
        {

        };

        public ActionResult EditCQ { get; private set; } = inParam =>
        {

        };
        public ActionResult DoubleRowCQ { get; private set; } = inParam =>
        {

        };
        #endregion


        #region 工作面



        public ActionResult AddGZM { get; private set; } = inParam =>
        {

        };

        public ActionResult EditGZM { get; private set; } = inParam =>
        {

        };
        public ActionResult DoubleRowGZM { get; private set; } = inParam =>
        {

        };
        #endregion

        #region 工作面



        public ActionResult AddXD { get; private set; } = inParam =>
        {

        };

        public ActionResult EditXD { get; private set; } = inParam =>
        {

        };
        public ActionResult DoubleRowXD { get; private set; } = inParam =>
        {

        };
        #endregion


        /// <summary>
        /// 后台加载
        /// </summary>
        void LoadData()
        {
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += (o, ea) =>
            {
                //no direct interaction with the UI is allowed from this method
                LoadData_();
                //CreateChartColumn("测试", strListx, strListy);
            };
            worker.RunWorkerCompleted += (o, ea) =>
            {
                //work has completed. you can now interact with the UI           
                ViewBag.IsBusy = false;
            };
            //set the IsBusy before you start the thread
            ViewBag.IsBusy = true;
            worker.RunWorkerAsync();
        }


        /// <summary>
        /// 初始化数据
        /// </summary>
        public void LoadData_()
        {
            AyThread.Instance.RunUI(() =>
            {
                //加载动画开始  
                ViewBag.IsBusy = true;
                var r = ConfigService.GetKM();
                S1Model.Kjbh = r.KMBH;
                S1Model.Dhhm = r.TELNUM;
                S1Model.Bflj = r.PATH;
                S1Model.Kjmc = r.KM;
                S1Model.Com = r.COM;


                #region CQ

                LoadDataCQ();

                #endregion

                #region GZM

                LoadDataGZM();

                #endregion

                #region XD
                LoadDataXD();

                #endregion
                ViewBag.IsBusy = false;
            });
        }

        /// <summary>
        /// 分开刷新 CQ
        /// </summary>
        void LoadDataCQ()
        {
            var cqr = ConfigService.GetCQ();
            DataSCQ.Clear();
            foreach (var r1 in cqr)
            {
                CQTable ct = new CQTable();
                ct.Cqmc = r1.CQMC;
                ct.Syzt = r1.ZT;
                ct.Ckmc = r1.COM;
                ct.Cqbh = r1.CQBH;
                DataSCQ.Add(ct);
            }
        }
        /// <summary>
        /// 分开刷新 GZM
        /// </summary>
        void LoadDataGZM()
        {
            var gzmr = ConfigService.GetGZM();
            DataSGZM.Clear();
            foreach (var r2 in gzmr)
            {
                GZMTable gt = new GZMTable();
                gt.Gzmmc = r2.GZMMC;
                gt.Gzmbh = r2.GZMBH;
                gt.Syzt = r2.ZT;
                gt.Sscq = r2.CQBH;
                gt.Cqmc = r2.CQMC;
                DataSGZM.Add(gt);
            }
        }
        /// <summary>
        /// 分开刷新 XD
        /// </summary>
        void LoadDataXD()
        {
            var xdr = ConfigService.GetXD(false);
            DataSXD.Clear();
            foreach (var r2 in xdr)
            {
                XDTable gt = new XDTable();
                gt.Sscq = r2.CQBH;
                gt.Xdbh = r2.XDBH;
                gt.Syzt = r2.ZT;
                gt.Xdmc = r2.XDMC;
                gt.Cqmc = r2.CQMC;
                DataSXD.Add(gt);
            }
        }

    }
}
